<?php
/**
 * Date: 15-04-22
 * Time: 上午10:49
 * @package  ajax
 * @subpackage  dunch
 * @author   Jerry
 */

quanli_require('module/base/customer/quanliCust.class.php'); //载入会员类
quanli_require('module/base/dunch/dunch.class.php'); //载入下午茶操作类
class ajax_coquetry
{

    public function execute()
    {

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1: //验证会员信息
                    $this->checkCustMaster();
                    break;
                case 2: //发送短信验证码
                    $this->send_sms();
                    break;
                case 3: //立即体验
                    $this->checkCode();
                    break;
                case 4: //套餐详情
                    $this->selInfo();
                    break;
                case 5: //获取商品信息、数量加入session
                    $this->goodsSession();
                    break;
                case 6: //提交订单
                    $this->createBill();
                    break;
                case 7: //吐槽
                    $this->tucao();
                    break;
                case 8: //开抢
                    $this->panic();
                    break;
                case 9: //add by allen.qiang 《allen.qiang@fistforward.com》MK-FUN-AC-004 下午茶项目迭代--下午茶会员权益
                    $this->ckeckRight();
                    break;
                case 10: //add by allen.qiang 《allen.qiang@fistforward.com》MK-FUN-AC-0041 登录或注册会员信息
                    $this->quickLogin();
                    break;


                case 100: //访问限制
                    $this->sayNo();
                    break;
                case 101: //验证登陆状态
                    $this->checkLogin();
                    break;
                case 102: //验证登陆状态
                    $this->save_WX_voice();
                    break;
                case 103: //验证登陆状态
                    $this->checkaliPay();
                break;

                default:
                    $this->toReturn('参数错误');
                    break;

            }

        } else {
            $this->toReturn('参数错误');
        }


    }

    /**
     * 返回信息
     * @param string $message
     * @param int $code
     * @param mixed $data
     */
    private function toReturn($message, $code = 400, $data = '')
    {

        die(json_encode(array('code' => $code, 'message' => $message, 'data' => $data)));

    }

    /*
     * Jerry 2015-04-22 判定是否有权益
     */
    public function sayNo()
    {
        global $G_SHOP;

        /*if (!$G_SHOP->currentPCustID) {
            $G_SHOP->currentPCustID = $_COOKIE['dunchCookie_pcustID'];
        }*/

        if ($G_SHOP->currentPCustID) {
            $checkSql = "select custID,pcustID from dunch_cust_master where pcustID=?  and  brandID=1";
            $quest = $G_SHOP->DBCA->stmt_query($checkSql, 's', $G_SHOP->currentPCustID);
            if (!empty($quest)) {
                foreach ($quest as $quest1) {
                    $questArray = array(
                        'custID' => $quest1->custID,
                        'pcustID' => $quest1->pcustID
                    );
                }
            }

            if (empty($questArray['pcustID'])) {
                //非权益用户
                returnJsonData("非特定会员", 200);
            } else {

                //保存openid
                if($ct_user = $G_SHOP->sessionCache_get('ct_user')){
                    $sql = "update dunch_cust_master set wx_openID=? ,wx_nickname=?,wx_headimgurl=? where pcustID=?";
                    $G_SHOP->DBCA->stmt_query($sql, 'ssss',$ct_user['openid'],$ct_user['nickname'],$ct_user['headimgurl'],$G_SHOP->currentPCustID);
                }

                //是否已有套餐
                if ($G_SHOP->sessionCache_get('info_ct_text')) {
                    returnJsonData("结算页面", 100);
                }
                returnJsonData("已登录", 300);
            }
        }
    }

    /*
     * 加减按钮
     * 验证会员信息
     */
    public function checkLogin()
    {
        global $G_SHOP;
        $cookieCustID = $_COOKIE['dunchCookie_custID'];
        $custID = $G_SHOP->currentCustomerID;
        if (!empty($cookieCustID) || !empty($custID)) {
            returnJsonData('YES', 200);
        } else {
            returnJsonData('NO', 400);
        }
    }

    /**
     * 验证会员信息
     */
    public function checkCustMaster()
    {

        global $G_SHOP;
        $phone = $_REQUEST['phone'];
        $checkSql = "select custID,pcustID from dunch_cust_master where custID=?  and  brandID=1";
        $quest = $G_SHOP->DBCA->stmt_query($checkSql, 's', $phone);
        if (!empty($quest)) {

            foreach ($quest as $quest1) {
                $questArray = array(
                    'custID' => $quest1->custID,
                    'pcustID' => $quest1->pcustID
                );
            }

        }
        if ($questArray['pcustID']) {

            returnJsonData('ok', 200, $questArray);
        } else {
            returnJsonData('ok', 400);
        }

        // returnJsonData('ok',200,$first_level_array);

    }

    /*
     * 发送短信验证码
     */
    public function send_sms()
    {
        global $G_SHOP;
        $phone = $_REQUEST['phone']; //手机号码
        //加载短信类
        quanli_include('module/base/common/quanliSms.class.php');

        //实例化短信类
        $quanliSms = new quanliSms();


        if (!isset($phone) || !is_mobile($phone)) {
            returnJsonData('参数错误');
        }


        $current_time = time(); //当前时间戳
        $last_send_time = $G_SHOP->sessionCache_get('sms_yzm_time_' . $phone); //上一次发放短信的时间戳
        if (!empty($last_send_time) && $current_time - $last_send_time < 30) {
            returnJsonData('手机短信已发送，请30s以后再试');
        }

        /**
         * ============================
         * 发送短信
         * ============================
         */

        //生成短信验证码
        $sms_yzm = $quanliSms->createSmsCode();

        //$sms_yzm = '1111';
        //生成短信内容
        $data_array = array(
            '$sms_yzm' => $sms_yzm,
            '$mobile' => $phone
        );
        $sms_content = $quanliSms->getSmsContent('dunch_mobile', $data_array);
        if (!$sms_content) {
            returnJsonData('短信初始化失败', 404);
        }

        //发送短信
        $result_array = $quanliSms->sendSMS( $phone, $sms_content );

        /**
         * ============================
         * 发送短信
         * ============================
         */
        // if ($result_array['code'] == 200) { //本地环境 注释掉
        //保存短信验证码到redis
        $G_SHOP->sessionCache_set('sms_yzm_' . $phone, md5($sms_yzm), 3600); //将验证码信息保存到memcache中去
        $G_SHOP->sessionCache_set('sms_yzm_time_' . $phone, time(), 3600);

        //    }
        returnJsonData($result_array, 200);
    }

    /*
     * Jerry 2015-04-22
     * 立即体验
     * 验证手机号and短信验证码是否匹配
     */
    public function checkCode()
    {

        global $G_SHOP;
        $phone = $_REQUEST['phone'];
        $code = $_REQUEST['code'];

        if(!$code){
            setcookie('dunchCookie_coquetry_phone', $phone, time() + 3600 * 24 * 7, '/');
            returnJsonData('需申请权益', 300);
        }

        //检查短信验证码
        $sms_ysm = $G_SHOP->sessionCache_get('sms_yzm_' . $phone);
        if (is_null($sms_ysm)) {
            returnJsonData('短信验证码已失效', 400);
        } elseif ($sms_ysm != md5($code)) {
            returnJsonData('短信验证码有误', 400);
        }

        $G_SHOP->sessionCache_remove('sms_yzm_' . $phone);
        $G_SHOP->sessionCache_remove('sms_yzm__time_' . $phone);
        //$zc=$this->quickLogin();
        //实例会员类
        $quanliCust = new quanliCust();

        //查询会员信息
        $arg_array = array(
            'custID' => $phone
        );
        $customer_array = $quanliCust->searchCustomerDetail($arg_array);
        //判定是否会员有权益
        if ($customer_array) {
            $custSql = "select is_right from  dunch_cust_master  WHERE pcustID=? ";
            $rightVal = $G_SHOP->DBCA->stmt_query($custSql, 's', $customer_array['pcustID']);
        }
        //edit by Jerry 2015-04-21
        if ($customer_array && $rightVal) {
            setcookie('dunchCookie_custID', $customer_array['custID'], time() + 3600 * 24 * 7, '/', 'fistforward.com'); //手机号存入cookie
            setcookie('dunchCookie_pcustID', $customer_array['pcustID'], time() + 3600 * 24 * 7, '/', 'fistforward.com'); //会员ID存入cookie
            //更新会员登录信息
            $quanliCust->shopCheckin($customer_array, 0);

            //套餐信息是否存在
            if ($G_SHOP->sessionCache_get('info_ct_text')) {
                //保存openid
                if($ct_user = $G_SHOP->sessionCache_get('ct_user')){
                    $sql = "update dunch_cust_master set wx_openID=? ,wx_nickname=?,wx_headimgurl=? where pcustID=?";
                    $G_SHOP->DBCA->stmt_query($sql, 'ssss',$ct_user['openid'],$ct_user['nickname'],$ct_user['headimgurl'],$G_SHOP->currentPCustID);
                }
                //用户已经选择过套餐，进入结算页面
                returnJsonData("登陆成功", 100);
            } else {
                //用户未选择套餐，进入套餐页
                returnJsonData('短信验证成功', 200);
            }
        } else {
            setcookie('dunchCookie_coquetry_phone', $phone, time() + 3600 * 24 * 7, '/');

            returnJsonData('需申请权益', 300);
        }


    }

    /*
     * 查询详情
     */
    public function selInfo()
    {
        global $G_SHOP;
        $package_id = $_REQUEST['package_id'];
//        $detalSql="select goodsName,frenchName,chineseBrief from dunch_package_detail join goods_sale on dunch_package_detail.product_code=goods_sale.product_code where package_id=?";
//        $datalQuest=$G_SHOP->DBCA->stmt_query($detalSql,'s',$package_id);
//        foreach($datalQuest as  $datalQuery){
//               $detal[]=array(
//                   'goodsName'=>$datalQuery->goodsName,
//                   'frenchName'=>$datalQuery->frenchName,
//                   'chineseBrief'=>$datalQuery->chineseBrief
//
//               );
//        }

        $packageSql = "SELECT package_image_3,package_image,package_remark,package_remark_2 FROM dunch_package where package_id=?";
        $packageQuest = $G_SHOP->DBCA->stmt_query($packageSql, 's', $package_id);

        foreach ($packageQuest as $packageQuery) {
            $cake = explode(',', $packageQuery->package_remark);
            $coffee = explode(',', $packageQuery->package_remark_2);
            $detal1 = array(
                'package_image_3' => $packageQuery->package_image_3,
                'package_image' => $packageQuery->package_image,
                'cakeName' => $cake[0],
                'coffeeName' => $coffee[0],
                'package_remark' => $cake[1],
                'package_remark_2' => $coffee[1]
            );
        }
        $return = array(
            'package' => $detal1,
//            'info'=>$detal,
        );
        returnJsonData($return, 200);
    }

    /*
     *  查询商品编号 数量
     *  商品信息存入session
     * 查询用户地址信息
     */
    public function goodsSession()
    {
        global $G_SHOP;

        $package_id = $_REQUEST['package_id'];
        $sale_id = $_REQUEST['saleid'];
        $num = $_REQUEST['num'];
        $saleSql = "SELECT package_num FROM dunch_sale where package_id=? and sale_id=?";
        $saleQuest = $G_SHOP->DBCA->stmt_query($saleSql, 'ss', $package_id, $sale_id);
        if ($saleQuest[0]->package_num < 1) {
            returnJsonData("哎呀，卖完了！不要灰心，明天早些就好啦", 400);
        }
        $detail = "select product_code from dunch_package_detail where package_id =?";
        $detailQuest = $G_SHOP->DBCA->stmt_query($detail, 's', $package_id);

        foreach ($detailQuest as $detailQuery) {

            $info[$sale_id][$package_id][] = array(
                'code' => $detailQuery->product_code,
                'num' => $num
            );
        }
        //把商品信息加入session or cookie start

        $G_SHOP->sessionCache_set("info_ct_text", $info, G_LOGINSESSION_TIMEOUT);
        //把商品信息加入session or cookie end
        returnJsonData("成功", 200);
    }

    /*
     * 提交-撒娇订单
     */
    public function createBill()
    {
        global $G_SHOP;
        if (!$G_SHOP->currentCustomerID) {
            $G_SHOP->currentCustomerID = $_COOKIE['dunchCookie_custID'];
        }
        if (!$G_SHOP->currentPCustID) {
            $G_SHOP->currentPCustID = $_COOKIE['dunchCookie_pcustID'];
        }
        $newNum = 0;
        $info = $G_SHOP->sessionCache_get('info_ct_text');
        foreach ($info as $goodsCode) {
            foreach ($goodsCode as $key => $goodsCode1) {
                foreach ($goodsCode1 as $goodsCode2) {
                    $newNum += $goodsCode2['num'];

                }
            }
        }

        //$search_ctbill_sql = 'SELECT bill_no FROM bill_master WHERE  billPlatform=3 AND pcustID=? AND send_date=? AND bill_status!=10';
        $search_ctbill_sql = 'SELECT a.bill_no FROM bill_master a,dunch_bill_detail b WHERE a.bill_no=b.bill_no AND b.coquetry_remark IS NOT NULL AND a.billPlatform=3 AND a.pcustID=? AND a.send_date=? AND a.bill_status!=10';
        $bill = $G_SHOP->DBCA->stmt_query($search_ctbill_sql, 'ss', $G_SHOP->currentPCustID, $_REQUEST['dateTime']);
        if($bill){
            returnJsonData("今天已经撒过娇啦！不要贪心~明天再来", 400);
            exit;
        }
        /*$packageSql = "select bill_totalAmount from bill_master where  billPlatform=3 and pcustID=? and send_date=? and bill_status!=10 and bill_status!=0";

        $custNum = 0;

        if($custPackage){
            foreach ($custPackage as $packageArray) {
                $custNum += $packageArray->bill_totalAmount;
            }
        }*/
        /*
         * by Jerry 是否有撒娇订单权益
         */
        $is_rightSql = "select is_coquetry from dunch_cust_master where pcustID=? ";
        $isVal = $G_SHOP->DBCA->stmt_query($is_rightSql, 's', $G_SHOP->currentPCustID);

        //限制购买次数
        /*if (($custNum + $newNum) > 4 && $isVal[0]->is_right != 1) {
            returnJsonData("每个会员每日限定2个套餐", 400);
        }*/

        //选中套餐信息
        foreach ($info as $key => $goodsCode) {
            foreach ($goodsCode as $goodsCode1) {
                $numSql = "select  count(1) as oldNum  from  dunch_sale WHERE sale_id=? and package_num>=" . $goodsCode1[0]['num'] . "";
                $oldNum = $G_SHOP->DBCA->stmt_query($numSql, 's', $key);
                if ($oldNum[0]->oldNum < 1) {
                    returnJsonData("哎呀，卖完了！不要灰心，明天早些就好啦", 400);
                }
            }
        }

        //$info = $G_SHOP->sessionCache_get('info_ct_text');
        foreach ($info as $key => $goodsCode) {
            $checkSql = "SELECT sale_date FROM dunch_sale where  sale_id=?";
            $chcekTime = $G_SHOP->DBCA->stmt_query($checkSql, 's', $key);
            if ($chcekTime[0]->sale_date != $_REQUEST['dateTime']) {
                returnJsonData("哎呀,<br/>已超过本日最晚下单时间，<br/>您可以返回选购明天的套餐", 400);
            }
        }

        $payId = 0; //支付方式ID
        $contacts = $_REQUEST['contacts']; //联系人
        $mobile = $_REQUEST['mobile']; //联系方式
        $m_address = $_REQUEST['m_address']; //收货地址
        $deliveryTimes = $_REQUEST['deliveryTimes']; //收货日期
        $dateTime = $_REQUEST['dateTime']; //收货时间

        $delivery = explode(" ", $deliveryTimes);//分隔上午 10:00~11:00
        $delivery = explode("~", $delivery[1]);

        $send_time_start = $delivery[0]; //配送开始日期
        $send_time_end = $delivery[1]; //配送结束日期
        $date_time = date('Y-m-d H:i:s', time());
//error_log($date_time);
//error_log($_REQUEST['dateTime']." 16:15:01");
        $nowTime = date('H:i:s', time());
        if ($date_time > $dateTime . " 16:00:01") {
            //returnJsonData("哎呀,<br/>已超过本日最晚下单时间，<br/>明天再来哦", 400);

        }

        $bill = array();
        $bill['custID'] = $G_SHOP->currentCustomerID;

        $bill['country'] = 440; //区id
        $bill['deliver_name'] = $contacts; //收货人
        $bill['deliver_address'] = $m_address; //收货人地址
        $bill['deliver_mobile'] = $mobile; //收货人手机

//        $bill['is_the_invoice'] = $_REQUEST['is_the_invoice'];
//        $bill['invoice_title_type'] = $_REQUEST['invoice_title_type'];//1：个人 2：单位
//        $bill['invoice_title'] = $_REQUEST['invoice_title']; //发票抬头

        //$bill['invoice_content'] = ''; //发票内容

        $bill['payTypeID'] = $payId; //支付方式ID
        $bill['payment_method_id'] = 0; //付款方式


        $bill['send_date'] = $dateTime; //收货日期
        $bill['send_start_time'] = $send_time_start; //配送开始时间
        $bill['send_end_time'] = $send_time_end; //配送结束时间

        $dunch = new dunch();
        $zt = $dunch->create_coquetry_bill($bill);
        if ($zt['code'] == 200) {

            foreach ($info as $key => $goodsCode) {
                foreach ($goodsCode as $goodsCode1) {
                    $numSql = "UPDATE dunch_sale  SET package_num=package_num-" . $goodsCode1[0]['num'] . " WHERE sale_id=? and package_num>=" . $goodsCode1[0]['num'] . "";
                    $oldNum = $G_SHOP->DBCA->stmt_query($numSql, 's', $key);
                    if ($oldNum == 0) {
                        returnJsonData("哎呀，卖完了！不要灰心，明天早些就好啦", 400);
                    }
                }

            }
            foreach ($info as $k => $bill_goods) {
                foreach ($bill_goods as $key => $bill_goods_2) {
                    $packageSql = "select package_name from  dunch_package where  package_id=? ";
                    $pageQuet = $G_SHOP->DBCA->stmt_query($packageSql, 's', $key);
                    $packageName = $pageQuet[0]->package_name;
                    $detailSql = "select goods_sale.* from  dunch_package_detail join goods_sale on dunch_package_detail.product_code=goods_sale.product_code  where package_id=?";
                    $detailQuest = $G_SHOP->DBCA->stmt_query($detailSql, 's', $key);
                    $postID = '';
                    $goodsName = '';
                    //查询采购单状态
                    $applySql = "SELECT status FROM  dunch_apply where   apply_date=?";
                    $applyQuest = $G_SHOP->DBCA->stmt_query($applySql, 's', $dateTime);
                    $isS = 0; //未入库
                    if ($applyQuest[0]->status == 3) {
                        $isS = 1; //已入库
                    }

                    foreach ($detailQuest as $detailQuery) {
                        if ($isS == 0) {
                            $updSql = "UPDATE dunch_stock  SET total_apply_amount=total_apply_amount-" . $bill_goods_2[0]['num'] . " WHERE product_code=? and stock_date=?";
                            $G_SHOP->DBCA->stmt_query($updSql, 'ss', $detailQuery->product_code, $dateTime);
                        } else {
                            $updSql = "UPDATE dunch_stock  SET total_apply_amount=total_paid_amount-" . $bill_goods_2[0]['num'] . " WHERE product_code=? and stock_date=?";
                            $G_SHOP->DBCA->stmt_query($updSql, 'ss', $detailQuery->product_code, $dateTime);
                        }
                        $logSql = "select stock_id from  dunch_stock  where  product_code=?";
                        $logQuest = $G_SHOP->DBCA->stmt_query($logSql, 's', $detailQuery->product_code);

                        $dunch_stock_log = new entity("dunch_stock_log");
                        $dunch_stock_log->stock_id = $logQuest[0]->stock_id;

                        $dunch_stock_log->log_type = 3;
                        $dunch_stock_log->amount = $bill_goods_2[0]['num'];
                        $G_SHOP->DBCA->save($dunch_stock_log);
                        $postID .= $detailQuery->postID . ",";
                        $goodsName .= $detailQuery->goodsName . "+";
                        $stock_id = $logQuest[0]->stock_id;
                    }


                    $postID = rtrim($postID, ',');
                    $goodsName = rtrim($goodsName, '+');

                    $dunch_bill_detail = new entity("dunch_bill_detail");
                    $dunch_bill_detail->bill_no = $zt['bill_no'];
                    $dunch_bill_detail->bill_code = $zt['bill_code'];
                    $dunch_bill_detail->package_id = $key;
                    $dunch_bill_detail->package_name = $packageName;
                    $dunch_bill_detail->goods_id_str = $postID;
                    $dunch_bill_detail->goods_name_str = $goodsName;
                    $dunch_bill_detail->buy_amount = $bill_goods_2[0]['num'];
                    $dunch_bill_detail->stock_id = $logQuest[0]->stock_id;
                    //撒娇内容
                    $dunch_bill_detail->coquetry_remark = $_REQUEST['coquetry_remark'];
                    $G_SHOP->DBCA->save($dunch_bill_detail);
                }
            }

            $billSql = "select pcustID,bill_receivable_money,goods_name_str,send_start_time,send_end_time,bill_totalAmount from bill_master where bill_code=?";
            $billQuest = $G_SHOP->DBCA->stmt_query($billSql, 's', $zt['bill_code']);

            $returnArray = array(
                "bill_no" => $zt['bill_no'],
                "pcustID" => $billQuest[0]->pcustID,
                "bill_receivable_money" => $billQuest[0]->bill_receivable_money,
                "goods_name_str" => $billQuest[0]->goods_name_str,
                "ip" => get_ip(),
                "bill_code" => $zt['bill_code'],
                "send_start_time" => $billQuest[0]->send_start_time,
                "send_end_time" => $billQuest[0]->send_end_time
            );


            returnJsonData("yes", 200, $returnArray);
        } else if ($zt['code'] == 404) {
            returnJsonData($zt['message'], 404);
        }

    }

    /*
     * 吐槽信息
     */
    public function tucao()
    {
        global $G_SHOP;
        $custID = $_REQUEST['custID']; //申请手机号
        $custName = $_REQUEST['custName']; //联系人名字
        $custMobile = $_REQUEST['custMobile']; //联系方式
        $areaID = $_REQUEST['areaID']; //区ID
        $areaText = $_REQUEST['areaText']; //区名称
        $content = $_REQUEST['content']; //详细地址

        $dunch_tucao = new entity("dunch_tucao");
        $dunch_tucao->custID = $custID;
        $dunch_tucao->custName = $custName;
        $dunch_tucao->mobile = $custMobile;
        $dunch_tucao->areaID = $areaID;
        $dunch_tucao->content = $areaText . $content;
        $dunch_tucao->platform = 1;
        $dunch_tucao->apply = 2;
        $G_SHOP->DBCA->save($dunch_tucao);
        returnJsonData("提交成功", 200);
    }

    /*
     * 开抢
     */
    public function panic()
    {
        global $G_SHOP;
        $sale_date = $_REQUEST['sale_date'];
        if (!$G_SHOP->currentPCustID) {
            $G_SHOP->currentPCustID = $_COOKIE['dunchCookie_pcustID'];
        }
        $custSearchSql = "select  is_right  from  dunch_cust_master WHERE pcustID=? ";
        $custSearchQuest = $G_SHOP->DBCA->stmt_query($custSearchSql, 's', $G_SHOP->currentPCustID);
        if ($custSearchQuest[0]->is_right == 1) {
            returnJsonData("一个会员仅限一次", 500);
        }


        $panicSql = "UPDATE dunch_sale  SET right_num=right_num-1 WHERE sale_date=? and right_num>=1";
        $zt = $G_SHOP->DBCA->stmt_query($panicSql, 's', $sale_date);


        if ($zt == 0) {
            returnJsonData("已抢光", 400);
        } else {
            $useSql = "UPDATE dunch_sale  SET use_num=use_num+1 WHERE sale_date=? ";
            $G_SHOP->DBCA->stmt_query($useSql, 's', $sale_date);

            $custSql = "UPDATE dunch_cust_master  SET is_right=1 WHERE pcustID=? ";
            $G_SHOP->DBCA->stmt_query($custSql, 's', $G_SHOP->currentPCustID);

            //加载短信类
            quanli_include('module/base/common/quanliSms.class.php');
            //实例化短信类
            $quanliSms = new quanliSms();

            /**
             * ============================
             * 发送短信
             * ============================
             */

            $sms_content = $quanliSms->getSmsContent('dunch_bespeak_sms', '');
            if (!$sms_content) {
                returnJsonData('短信初始化失败', 404);
            }
            if (!$G_SHOP->currentCustomerID) {
                $G_SHOP->currentCustomerID = $_COOKIE['dunchCookie_custID'];
            }
            //发送短信
            $result_array = $quanliSms->sendSMS($G_SHOP->currentCustomerID, $sms_content);

            returnJsonData("预约成功", 200);
        }
    }

    /*
     *add by Jerry 撒娇会员权益 start
     */
    public function ckeckRight()
    {
        global $G_SHOP;
        if (!$G_SHOP->currentPCustID) {
            $G_SHOP->currentPCustID = $_COOKIE['dunchCookie_pcustID'];
        }
        $custSql = "select is_right from  dunch_cust_master  WHERE pcustID=? ";
        $rightVal = $G_SHOP->DBCA->stmt_query($custSql, 's', $G_SHOP->currentPCustID);
        returnJsonData($rightVal[0]->is_right, 200);

    }

    /*
     *撒娇会员权益 end
     */


    /*
     *add by allen.qiang 《allen.qiang@fistforward.com》MK-FUN-AC-0041 登录或注册下午茶会员 start
     */

    public function quickLogin()
    {
        global $G_SHOP;
        //检查参数
        if (!isset($_REQUEST['phone']) || empty($_REQUEST['phone'])) {
            returnJsonData('缺少参数');
        }

        $guest = 1; //前台登录会员类型 1、无密码登录的用户 0、会员 2、有密码但没使用

        //实例会员类
        $quanliCust = new quanliCust();

        //查询会员信息
        $arg_array = array(
            'custID' => $_REQUEST['phone']
        );
        $customer_array = $quanliCust->searchCustomerDetail($arg_array);
        if ($customer_array) {
            $dunch_cust_master = new entity('dunch_cust_master');
            $dunch_cust_master->pcustID = $customer_array['pcustID'];
            $G_SHOP->DBCA->fetch($dunch_cust_master);
            $dunch_cust_master->pcustID = $customer_array['pcustID'];
            $dunch_cust_master->custID = $customer_array['custID'];
            $dunch_cust_master->brandID = 1;
            $G_SHOP->DBCA->save($dunch_cust_master);

        } else {

            $pwd = get_random_string(6);

            //前台注册会员信息
            $args_array = array(
                'custID' => $_REQUEST['phone'],
                'pwd' => $pwd,
                'pwd2' => $pwd,
                'custSource' => 1,
                'custType' => 2
            );
            $result_array = $quanliCust->register($args_array);
            if ($result_array['code'] != 200) {
                returnJsonData($result_array['message'], $result_array['code']);
            }
            //查询会员信息
            $arg_array = array(
                'custID' => $_REQUEST['phone']
            );
            $customer_array = $quanliCust->searchCustomerDetail($arg_array);
            $dunch_cust_master = new entity('dunch_cust_master');
            $dunch_cust_master->pcustID = $customer_array['pcustID'];
            $G_SHOP->DBCA->fetch($dunch_cust_master);
            $dunch_cust_master->pcustID = $customer_array['pcustID'];
            $dunch_cust_master->custID = $customer_array['custID'];
            $dunch_cust_master->brandID = 1;
            $G_SHOP->DBCA->save($dunch_cust_master);
            $customer_array = $result_array['data'];
            /**
             * ============================
             * 发送短信
             * ============================
             */
            //加载短信类
            quanli_include('module/base/common/quanliSms.class.php');

            //实例化短信类
            $quanliSms = new quanliSms();
            //生成短信验证码


            //生成短信内容
            $data_array = array(
                '$pwd' => $pwd
            );
            $sms_content = $quanliSms->getSmsContent('dunch_register', $data_array);
            if (!$sms_content) {
                returnJsonData('短信初始化失败', 404);
            }

            //发送短信
            $result_array = $quanliSms->sendSMS($_REQUEST['phone'], $sms_content);

            /**
             * ============================
             * 发送短信
             * ============================
             */
        }
        $pcustsql = 'select pcustID from dunch_cust_master  where custID=?';
        $pcustArray = $G_SHOP->DBCA->stmt_query($pcustsql, 's', $_REQUEST['phone']);
        setcookie('dunchCookie_custID', $_REQUEST['phone'], time() + 3600 * 24 * 7, '/', 'fistforward.com'); //手机号存入cookie
        setcookie('dunchCookie_pcustID', $pcustArray[0]->pcustID, time() + 3600 * 24 * 7, '/', 'fistforward.com'); //会员ID存入cookie
        //更新会员登录信息
        $quanliCust->shopCheckin($customer_array, $guest);
        returnJsonData('短信验证成功', 200);
    }

    /*
     *add by allen.qiang 《allen.qiang@fistforward.com》MK-FUN-AC-0041 登录或注册下午茶会员 end
     */


    //保存微信音频文件
    function save_WX_voice(){
        global $G_SHOP;

        $media_id = $_REQUEST['media_id'];

        //保存订单与音频media_id关系
        $wx_voice = new entity('dunch_wx_voice');
        $wx_voice->bill_code = $_REQUEST['code'];
        if($G_SHOP->DBCA->fetch($wx_voice)){
            returnJsonData('抱歉！您已经上传过录音', 400);
            exit;
        }else{
            $wx_voice->media_id = $media_id;
            $G_SHOP->DBCA->save($wx_voice);
        }

        // access_token 应该全局存储与更新，以下代码以写入到文件中做示例
        //$data = json_decode(file_get_contents("access_token.json"));

        //$access_token = $data->access_token;
        //$path_dir = '/ebsig/inonesoft/nginx/htdocs/dunch/coquetry/voice/';

        //$url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$access_token."&media_id=".$media_id;
        //file_put_contents($path_dir.$media_id.".amr",file_get_contents($url));
        //exec("http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$access_token."&media_id=".$media_id)

        //发送图文消息
        /*http://xxx.xxx.com/dunch/coquetry/record_play.php?subsite=dunch&code=1050426103256 /*/
        require('dunch/coquetry/wx.class.php');
        require('dunch/coquetry/jssdk.php');

        $jssdk = new JSSDK();
        $acc_token = $jssdk->getAccessToken();

        //收消息人的openId
        $bill = new entity('bill_master');
        $bill->bill_code = $_REQUEST['code'];
        if($G_SHOP->DBCA->fetch($bill)){

            $cust = new entity('dunch_cust_master');
            $cust->pcustID = $bill->pcustID;
            if($G_SHOP->DBCA->fetch($cust)){
                $touser = $cust->wx_openID;//'ooBCYjskaoJHrji845bgB-qgxe6Y';
                $content = Array(
                    'title'=>'【声音胶囊】有人已经为你买单了，但~似乎ta还有话想说',
                    'description'=>'听听TA说了什么！   有劳',
                    'url'=>'http://121.43.104.7:91/dunch/coquetry/record_play.php?subsite=dunch&code='.$_REQUEST['code'],
                    'picurl'=>'http:\/\/121.43.104.7\/dunch\/theme\/xth1\/images\/ct\/coquetry_img.jpg'
                );

                $wx = new wx_class($acc_token);
                $res = $wx->_reply_news_customer($touser,$content);
                error_log(print_r($res,1));
            }
        }
        returnJsonData('成功', 200);
    }

    function checkaliPay(){
        global $G_SHOP;
        $code = $_REQUEST['code'];

        $bill = new entity('bill_master');
        $bill->bill_code = $code;
        $bill->pay_status = 1;
        if($G_SHOP->DBCA->fetch($bill)){
            returnJsonData('订单已支付', 400);
        }
        returnJsonData('成功', 200);
    }
}
